home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Program: DTS.Lib
- ** File: window.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1991 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "DTS.Lib2.h"
- #include "DTS.Lib.Common.h"
- #include "DTS.Lib.protos.h"
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __MOVIES__
- #include <Movies.h>
- #endif
-
- #ifndef THINK_C
- #ifndef __SYSEQU__
- #include <SysEqu.h>
- #endif
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- short gBeginUpdateNested;
-
- static RgnHandle gKeepUpdateRgn;
- static WindowPtr gOldPort;
-
- extern short gPrintPage; /* Non-zero means we are printing. */
- extern short gMinVersion, gMaxVersion;
- extern Boolean gInBackground;
-
- RgnHandle gCursorRgn;
- /* The current cursor region. The initial cursor region is an empty
- ** region, which will cause WaitNextEvent to generate a mouse-moved
- ** event, which will cause us to set the cursor for the first time. */
-
- static Cursor gCursor;
- CursPtr gCursorPtr;
- /* The current cursor that applies to gCursorRgn. These values
- ** are here to shorten the re-processing time for determining the
- ** correct cursor after an event. This is specifically so that characters
- ** can be typed into the TextEdit control faster. If we spend a great
- ** deal of time per-event recalculating the cursor region, text entry for
- ** the TextEdit control slows down considerably. If you want to override
- ** the time savings because you are changing the cursor directly, either
- ** set gCursorPtr to nil, or call DoSetCursor to set the cursor.
- ** DoSetCursor simply sets gCursorPtr to nil, as well as setting
- ** the cursor. */
-
- static Rect OldLocWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo);
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* This function creates a new application window. An application window
- ** contains a document which is referenced by a handle in the refCon field. */
-
- #pragma segment Window
- OSErr DoNewWindow(FileRecHndl frHndl, WindowPtr *retWindow,
- WindowPtr relatedWindow, WindowPtr behind)
- {
- WindowPtr oldPort, window;
- FileRecHndl ffrHndl;
- ControlHandle ctl;
- Rect ctlRect, contRect, userState, sizeInfo;
- short attributes, h, v, wkind, wwkind;
- OSErr err;
- GetDocWindowProcPtr proc;
-
- if (!frHndl) return(noErr);
- attributes = (*frHndl)->fileState.attributes;
-
- err = noErr;
- GetPort(&oldPort);
-
- proc = (*frHndl)->fileState.getDocWindow;
- if (!proc)
- proc = GetStaggeredWindow;
- if (attributes & kwOpenAtOldLoc)
- proc = GetOldLocWindow;
-
- SetRect(&sizeInfo, 0, 0, 0, 0);
-
- wkind = (attributes & (kwIsPalette | kwIsModalDialog));
- if (behind) {
- for (;;) {
- if (!(behind = GetNextWindow(behind, 0))) break;
- /* break if behind all windows. behind is nil, so that's what we have. */
- ffrHndl = (FileRecHndl)GetWRefCon(behind);
- wwkind = ((*ffrHndl)->fileState.attributes & (kwIsPalette | kwIsModalDialog));
- if (wkind >= wwkind) {
- behind = GetPreviousWindow(behind);
- break;
- }
- }
- }
-
- window = (*proc)((*frHndl)->fileState.windowID, nil, false, relatedWindow,
- behind, true, sizeInfo, (long)frHndl);
- if (window) {
- SetPort(window);
- NewWindowTitle(window, nil);
-
- (*frHndl)->fileState.window = window;
-
- (*frHndl)->fileState.hArrowVal = 16; /* Default arrow value is 16. */
- (*frHndl)->fileState.vArrowVal = 16;
-
- (*frHndl)->fileState.windowSizeBounds.left = kMinWindowWidth;
- (*frHndl)->fileState.windowSizeBounds.top = kMinWindowHeight;
- (*frHndl)->fileState.windowSizeBounds.right = kMaxWindowWidth;
- (*frHndl)->fileState.windowSizeBounds.bottom = kMaxWindowHeight;
- /* Default min/max window size for growIcon. */
-
- if (!(*frHndl)->d.doc.fhInfo.hDocSize) {
- h = window->portRect.right;
- h -= (*frHndl)->fileState.leftSidebar; /* Default document size is */
- if (attributes & kwHScroll) /* content less leftSidebar */
- h -= 15; /* value less scrollbars. */
- (*frHndl)->d.doc.fhInfo.hDocSize = h;
- }
- if (!(*frHndl)->d.doc.fhInfo.vDocSize) {
- v = window->portRect.bottom;
- v -= (*frHndl)->fileState.topSidebar; /* We don't have to initialize the page */
- if (attributes & kwVScroll) /* values since the scrollbars won't be */
- v -= 15; /* active until the window is resized */
- (*frHndl)->d.doc.fhInfo.vDocSize = v; /* or these values are set elsewhere. */
- }
-
- if (attributes & kwHScroll) { /* Caller wants a horizontal scrollbar... */
- ctlRect = window->portRect;
- ctlRect.left += (*frHndl)->fileState.leftSidebar;
- ctlRect.left += (*frHndl)->fileState.hScrollIndent;
- --ctlRect.left;
- ++ctlRect.right;
- ctlRect.top = ++ctlRect.bottom - 16;
- if (attributes & (kwHScrollLessGrow - kwHScroll + kwGrowIcon))
- ctlRect.right -= 15;
- OffsetRect(&ctlRect, 0, -16384);
- ctl = NewControl(window, &ctlRect, nil, true, 0, 0, 0, scrollBarProc, 0L);
- if (ctl)
- (*frHndl)->fileState.hScroll = ctl;
- else
- err = memFullErr;
- }
-
- if (!err) {
- if (attributes & kwVScroll) { /* Caller wants a vertical scrollbar... */
- ctlRect = window->portRect;
- ctlRect.top += (*frHndl)->fileState.topSidebar;
- ctlRect.top += (*frHndl)->fileState.vScrollIndent;
- --ctlRect.top;
- ++ctlRect.bottom;
- ctlRect.left = ++ctlRect.right - 16;
- if (attributes & (kwVScrollLessGrow - kwVScroll + kwGrowIcon))
- ctlRect.bottom -= 15;
- OffsetRect(&ctlRect, 0, -16384);
- ctl = NewControl(window, &ctlRect, nil, true, 0, 0, 0, scrollBarProc, 0L);
- if (ctl)
- (*frHndl)->fileState.vScroll = ctl;
- else
- err = memFullErr;
- }
- }
-
- if (!err) {
- err = DoInitContent(frHndl, window);
- if (!err) {
- contRect = GetWindowContentRect(window);
- userState = mDerefWStateData(window)->userState;
- /* Cache this. ShowWindow offscreen messes it up. We want to keep
- ** whatever it is because some other function may be tweeking stdState
- ** and userState so that window position and zoom state can be saved
- ** along with a document. */
-
- MoveWindow(window, 16384, 16384, false);
- /* When an invisible window is added, it isn't the frontmost window.
- ** This causes a problem when we make it visible. By moving it
- ** offscreen, we can make it visible without the user seeing that it
- ** isn't the top window. */
-
- if (gPrintPage) {
- ShowWindow(window); /* Window now visible. */
- MoveWindow(window, 16384, 16384, true); /* Window now on top. */
- } /* If we are printing, we want to leave the window offscreen, since we
- ** don't want it seen. We do need a visible window when printing so
- ** PrintMonitor can get the document name. */
-
- if (!gPrintPage) {
- if (attributes & kwVisible)
- ShowHide(window, true);
- CleanSendBehind(window, behind);
- MoveWindow(window, contRect.left, contRect.top, false);
- }
-
- mDerefWStateData(window)->userState = userState;
- /* The ShowWindow metrics we did cause the userState to change. Put it
- ** back the way it was before we started messing around with the window. */
-
- AdjustScrollBars(window);
- }
- }
- if (err) {
- DisposeAnyWindow(window);
- (*frHndl)->fileState.window = window = nil;
- }
- }
- else err = memFullErr;
-
- SetPort(oldPort);
- if (retWindow)
- *retWindow = window;
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This function updates the window title to reflect the new document name.
- ** The new document name is stored in the fileState portion of the document.
- ** This is automatically set to 'Untitled # N' for new documents, and is
- ** updated when a user does a save-as. If the window that is being opened
- ** should have the resource name, then set the new document name to an
- ** empty string prior to calling DoNewWindow. When this is called by
- ** DoNewWindow, the SetWTitle will be suppressed. */
-
- #pragma segment Window
- void NewWindowTitle(WindowPtr window, StringPtr altTitle)
- {
- FileRecHndl frHndl;
- Str255 wTitle;
-
- if (window) {
- if (altTitle) {
- pcpy(wTitle, altTitle);
- SetWTitle(window, wTitle);
- }
- else if (frHndl = (FileRecHndl)GetWRefCon(window)) {
- pcpy(wTitle, (*frHndl)->fileState.fss.name);
- if (*wTitle)
- SetWTitle(window, wTitle);
- }
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Close all the windows. This is called prior to quitting the application.
- ** This function returns true if all windows were closed. The user may decide
- ** to abort a save, thus stopping the closing of the windows. If the user
- ** does this, false will be returned, indicating that all windows were not
- ** closed after all. */
-
- #pragma segment Window
- Boolean DisposeAllWindows(void)
- {
- WindowPtr window, nextWindow;
- FileRecHndl frHndl;
- short attr;
-
- #ifdef __SYSEQU__
- window = *(WindowPtr *)WindowList;
- #else
- window = (WindowPtr)WindowList;
- #endif
-
- for (;window; window = nextWindow) {
-
- nextWindow = (WindowPtr)(((WindowPeek)window)->nextWindow);
-
- if (!IsDAWindow(window)) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- attr = (*frHndl)->fileState.attributes;
- if (attr & kwIsPalette) continue;
- /* Closing of all of the windows may be stopped at a
- ** dirty document, so don't close the palettes. */
- }
-
- if (!DisposeOneWindow(window, kQuit)) return(false);
- /* When DisposeOneWindow returns false, this means that the window
- ** didn't close. The only cause of this is if the window had a
- ** document that needed saving, and the user cancelled the save.
- ** If the windows succeed in getting closed, then we are
- ** returned true. */
- }
-
- return(true);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Closes one window. This window may be an application window, or it may be
- ** a system window. If it is an application window, it may have a document
- ** that needs saving. */
-
- #pragma segment Window
- Boolean DisposeOneWindow(WindowPtr window, short saveMode)
- {
- FileRecHndl frHndl;
- short attr;
- OSErr err;
-
- if (window) {
- if (!IsDAWindow(window)) {
- /* First, if the window is an application window, try saving
- ** the document. Remember that the user may cancel the save.
- */
-
- if (frHndl = (FileRecHndl)GetWRefCon(window)) {
-
- attr = (*frHndl)->fileState.attributes;
- if (attr & kwHideOnClose) {
- HideWindow(window);
- HiliteWindows();
- WindowGoneFixup(window);
- return(true);
- }
-
- if (IsAppWindow(window)) {
- err = SaveDocument(frHndl, window, saveMode);
- if (err) {
- if (err != userCanceledErr)
- Alert(rErrorAlert, (ModalFilterProcPtr)AlertFilter);
- return(false);
- } /* Stop closing windows on error or user cancel. */
- }
-
- err = DoFreeWindow(frHndl, window);
- if (err) return(false);
-
- DisposeDocument(frHndl);
- /* If everything is cool, dispose of the document. */
- }
- }
- DisposeAnyWindow(window);
- HiliteWindows();
- WindowGoneFixup(window);
- /* Give the application a chance to do any related tasks. */
- }
-
- return(true);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- WindowPtr SetFilePort(FileRecHndl frHndl)
- {
- WindowPtr oldPort;
-
- GetPort(&oldPort);
- if (frHndl)
- SetPort((*frHndl)->fileState.window);
- return(oldPort);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoResizeWindow(WindowPtr window, short oldh, short oldv)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- short attributes;
- Boolean growIconSpace;
- Rect portRct, rct;
- ControlHandle hScroll, vScroll;
- RgnHandle updateRgn;
-
- if (!window) return;
-
- frHndl = (FileRecHndl)GetWRefCon(window);
- oldPort = SetFilePort(frHndl);
- attributes = (*frHndl)->fileState.attributes;
- growIconSpace = (attributes & (kwGrowIcon | (kwHScrollLessGrow - kwHScroll) | (kwVScrollLessGrow - kwVScroll)));
- /* growIconSpace true if window has grow icon or a blank space for one. */
-
- SetOrigin(0, 0);
- portRct = window->portRect;
-
- if (growIconSpace) {
- rct.left = (rct.right = oldh) - 15;
- rct.top = (rct.bottom = oldv) - 15;
- EraseRect(&rct);
- InvalRect(&rct);
- rct = portRct;
- rct.left = rct.right - 15;
- rct.top = rct.bottom - 15;
- EraseRect(&rct);
- }
-
- SetOrigin(0, -16384);
- if (hScroll = (*frHndl)->fileState.hScroll) {
- HideControl(hScroll);
- rct = (*hScroll)->contrlRect;
- MoveControl(hScroll, rct.left, portRct.bottom - 15 - 16384);
- SizeControl(hScroll, rct.right - rct.left + (portRct.right - portRct.left - oldh), 16);
- }
- if (vScroll = (*frHndl)->fileState.vScroll) {
- HideControl(vScroll);
- rct = (*vScroll)->contrlRect;
- MoveControl(vScroll, portRct.right - 15, rct.top);
- SizeControl(vScroll, 16, rct.bottom - rct.top + (portRct.bottom - portRct.top - oldv));
- }
-
- AdjustScrollBars(window);
-
- if (hScroll)
- ShowControl(hScroll);
- if (vScroll)
- ShowControl(vScroll);
-
- SetOrigin(0, 0);
- if (attributes & kwGrowIcon)
- DoDrawGrowIcon(window, false, false);
-
- BeginContent(window);
- DoResizeContent(window, oldh, oldv);
- updateRgn = NewRgn();
- CopyRgn(((WindowPeek)window)->updateRgn, updateRgn);
- EndContent(window);
- UnionRgn(updateRgn, ((WindowPeek)window)->updateRgn, ((WindowPeek)window)->updateRgn);
- DisposeRgn(updateRgn);
-
- SetPort(oldPort);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoUpdateSeparate(WindowPtr window, RgnHandle *contRgn, RgnHandle *frameRgn)
- {
- RgnHandle urgn, wrgn;
-
- *contRgn = *frameRgn = nil;
- if (!window) return;
-
- urgn = DoCalcFrameRgn(window);
- wrgn = NewRgn();
-
- DiffRgn(((WindowPeek)window)->updateRgn, urgn, wrgn);
- if (!EmptyRgn(wrgn)) {
- *contRgn = wrgn;
- wrgn = NewRgn();
- }
- SectRgn(((WindowPeek)window)->updateRgn, urgn, wrgn);
-
- if (!EmptyRgn(wrgn))
- *frameRgn = wrgn;
- else
- DisposeRgn(wrgn);
-
- DisposeRgn(urgn);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void BeginContent(WindowPtr window)
- {
- RgnHandle updateRgn, frameRgn;
- Point contOrg;
-
- if (!gBeginUpdateNested++) {
- GetPort(&gOldPort);
- if (window) {
- SetPort(window);
- CopyRgn(updateRgn = ((WindowPeek)window)->updateRgn, gKeepUpdateRgn = NewRgn());
- frameRgn = DoCalcFrameRgn(window);
- DiffRgn(((WindowPeek)window)->contRgn, frameRgn, updateRgn);
- DisposeRgn(frameRgn);
- BeginUpdate(window);
- GetContentOrigin(window, &contOrg);
- SetOrigin(contOrg.h, contOrg.v);
- }
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void EndContent(WindowPtr window)
- {
- if (!--gBeginUpdateNested) {
- if (window) {
- EndUpdate(window);
- UnionRgn(gKeepUpdateRgn, ((WindowPeek)window)->updateRgn, ((WindowPeek)window)->updateRgn);
- DisposeRgn(gKeepUpdateRgn);
- }
- SetPort(gOldPort);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void AdjustScrollBars(WindowPtr window)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- ControlHandle hScroll, vScroll;
- Rect portRct;
- Point keepOrg;
- short h, v, maxVal, val;
- DocScrollBarProc proc;
-
- if (!window) return;
-
- oldPort = SetFilePort(frHndl = (FileRecHndl)GetWRefCon(window));
- portRct = window->portRect;
-
- keepOrg.h = portRct.left;
- keepOrg.v = portRct.top;
-
- portRct.left += (*frHndl)->fileState.leftSidebar;
- portRct.top += (*frHndl)->fileState.topSidebar;
-
- hScroll = (*frHndl)->fileState.hScroll;
- vScroll = (*frHndl)->fileState.vScroll;
-
- SetOrigin(0, -16384);
-
- if ((maxVal = (*frHndl)->d.doc.fhInfo.hDocSize) > 0) {
- h = portRct.right - portRct.left;
- if (vScroll)
- h -= 15;
- maxVal -= h;
- if (maxVal < 0)
- maxVal = 0;
- if (hScroll) {
- proc = (DocScrollBarProc)GetCRefCon(hScroll);
- if (proc)
- (*proc)(frHndl, hScroll, kscrollHAdjust, h);
- else {
- if (maxVal < (val = GetCtlValue(hScroll)))
- maxVal = val;
- if ((*hScroll)->contrlMax != maxVal) {
- (*hScroll)->contrlMax = maxVal;
- DoDraw1Control(hScroll, true);
- }
- h -= (val = (*frHndl)->fileState.hArrowVal);
- if (h < val)
- h = val;
- (*frHndl)->fileState.hPageVal = h;
- }
- }
- }
-
- if ((maxVal = (*frHndl)->d.doc.fhInfo.vDocSize) > 0) {
- v = portRct.bottom - portRct.top;
- if (hScroll)
- v -= 15;
- maxVal -= v;
- if (maxVal < 0)
- maxVal = 0;
- if (vScroll) {
- proc = (DocScrollBarProc)GetCRefCon(vScroll);
- if (proc)
- (*proc)(frHndl, vScroll, kscrollVAdjust, v);
- else {
- if (maxVal < (val = GetCtlValue(vScroll)))
- maxVal = val;
- if ((*vScroll)->contrlMax != maxVal) {
- (*vScroll)->contrlMax = maxVal;
- DoDraw1Control(vScroll, true);
- }
- v -= (val = (*frHndl)->fileState.vArrowVal);
- if (v < val)
- v = val;
- (*frHndl)->fileState.vPageVal = v;
- }
- }
- }
-
- SetOrigin(keepOrg.h, keepOrg.v);
- SetPort(oldPort);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void GetContentOrigin(WindowPtr window, Point *contOrg)
- {
- FileRecHndl frHndl;
- ControlHandle ctl;
- DocScrollBarProc proc;
-
- contOrg->h = contOrg->v = 0;
-
- if (window) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- if (ctl = (*frHndl)->fileState.hScroll) {
- contOrg->h = GetCtlValue(ctl);
- if (proc = (DocScrollBarProc)GetCRefCon(ctl))
- contOrg->h = (*proc)(frHndl, ctl, kscrollGetHOrigin, 0);
- }
- if (ctl = (*frHndl)->fileState.vScroll) {
- contOrg->v = GetCtlValue(ctl);
- if (proc = (DocScrollBarProc)GetCRefCon(ctl))
- contOrg->v = (*proc)(frHndl, ctl, kscrollGetVOrigin, 0);
- }
- contOrg->h -= (*frHndl)->fileState.leftSidebar;
- contOrg->v -= (*frHndl)->fileState.topSidebar;
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void SetContentOrigin(WindowPtr window, long newh, long newv)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- ControlHandle hScroll, vScroll;
- short topSide, leftSide, max, dh, dv;
- Point old, contOrg;
- RgnHandle updateRgn;
- Rect contRct;
-
- if (!window) return;
-
- oldPort = SetFilePort(frHndl = (FileRecHndl)GetWRefCon(window));
- hScroll = (*frHndl)->fileState.hScroll;
- vScroll = (*frHndl)->fileState.vScroll;
- GetContentOrigin(window, &old);
- GetContentRect(window, &contRct);
-
- SetOrigin(0, -16384);
- topSide = (*frHndl)->fileState.topSidebar;
- leftSide = (*frHndl)->fileState.leftSidebar;
-
- if (!hScroll)
- newh = kwNoChange;
- if (newh != kwNoChange) {
- if (newh != kwBotScroll)
- newh += leftSide;
- if (newh < 0)
- newh = 0;
- if (newh > (max = GetCtlMax(hScroll)))
- newh = max;
- max = (*frHndl)->d.doc.fhInfo.hDocSize - (contRct.right - contRct.left);
- if (max < 0)
- max = 0;
- if (newh > max)
- newh = max;
- if ((*hScroll)->contrlValue != newh) {
- (*hScroll)->contrlValue = newh;
- DoDraw1Control(hScroll, true);
- }
- newh -= leftSide;
- }
-
- if (!vScroll)
- newv = kwNoChange;
- if (newv != kwNoChange) {
- if (newv != kwBotScroll)
- newv += topSide;
- if (newv < 0)
- newv = 0;
- if (newv > (max = GetCtlMax(vScroll)))
- newv = max;
- max = (*frHndl)->d.doc.fhInfo.vDocSize - (contRct.bottom - contRct.top);
- if (max < 0)
- max = 0;
- if (newv > max)
- newv = max;
- if ((*vScroll)->contrlValue != newv) {
- (*vScroll)->contrlValue = newv;
- DoDraw1Control(vScroll, true);
- }
- newv -= topSide;
- }
-
- AdjustScrollBars(window);
-
- dh = dv = 0;
- if (newh != kwNoChange)
- dh = old.h - newh;
- if (newv != kwNoChange)
- dv = old.v - newv;
-
- BeginContent(window);
- ScrollRect(&(window->portRect), dh, dv, updateRgn = NewRgn());
- EndContent(window);
-
- OffsetRgn(((WindowPeek)window)->updateRgn, dh, dv);
- /* We want to add the scrolled-in area into the updateRgn. We
- ** also want to keep the old area. The old update area is
- ** no longer mapped to the same location, due to the scroll,
- ** so offset it by the amount scrolled. Once it is offset, we
- ** can add our new update portion to the updateRgn. */
-
- GetContentOrigin(window, &contOrg);
- SetOrigin(contOrg.h, contOrg.v);
- InvalRgn(updateRgn);
- DisposeRgn(updateRgn);
-
- SetOrigin(old.h, old.v);
- SetPort(oldPort); /* Put things back the way we found them. */
-
- DoScrollFrame(window, dh, dv); /* There may be changes in the frame due to scrolling. */
-
- DoSetCursor(nil); /* Cursor region may be invalid due to
- ** content being scrolled. Force it to
- ** be recalculated. */
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void GetContentRect(WindowPtr window, Rect *contRct)
- {
- FileRecHndl frHndl;
- ControlHandle ctl;
-
- SetRect(contRct, 0, 0, 0, 0);
-
- if (window) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- *contRct = window->portRect;
- if (ctl = (*frHndl)->fileState.hScroll)
- contRct->bottom -= 15;
- if (ctl = (*frHndl)->fileState.vScroll)
- contRct->right -= 15;
- contRct->top += (*frHndl)->fileState.topSidebar;
- contRct->left += (*frHndl)->fileState.leftSidebar;
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void SetDocSize(FileRecHndl frHndl, long hSize, long vSize)
- {
- if (frHndl) {
- if (hSize >= 0)
- (*frHndl)->d.doc.fhInfo.hDocSize = hSize;
- if (vSize >= 0)
- (*frHndl)->d.doc.fhInfo.vDocSize = vSize;
- AdjustScrollBars((*frHndl)->fileState.window);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void SetSidebarSize(FileRecHndl frHndl, short newLeft, short newTop)
- {
- WindowPtr oldPort, window;
- Rect portRct, rct;
- Point contOrg;
- short oldLeft, oldTop, dh, dv;
- ControlHandle ctl;
- RgnHandle updateRgn;
- DrawFrameProcPtr proc;
-
- if (window = (*frHndl)->fileState.window) {
- oldPort = SetFilePort(frHndl);
- portRct = window->portRect;
- SetOrigin(0, -16384); /* Prepare to modify (redraw) document scrollbars. */
- }
-
- oldLeft = (*frHndl)->fileState.leftSidebar;
- oldTop = (*frHndl)->fileState.topSidebar;
-
- dh = 0;
- if (newLeft != kwNoChange) {
- if (ctl = (*frHndl)->fileState.hScroll) {
- HideControl(ctl);
- rct = (*ctl)->contrlRect;
- rct.left += (newLeft - oldLeft);
- MoveControl(ctl, rct.left, rct.top);
- SizeControl(ctl, rct.right - rct.left, 16);
- ShowControl(ctl);
- }
- dh = newLeft - oldLeft;
- }
-
- dv = 0;
- if (newTop != kwNoChange) {
- if (ctl = (*frHndl)->fileState.vScroll) {
- HideControl(ctl);
- rct = (*ctl)->contrlRect;
- rct.top += (newTop - oldTop);
- MoveControl(ctl, rct.left, rct.top);
- SizeControl(ctl, 16, rct.bottom - rct.top);
- ShowControl(ctl);
- }
- dv = newTop - oldTop;
- }
-
- if (dh < 0)
- (*frHndl)->fileState.leftSidebar = newLeft;
- if (dv < 0)
- (*frHndl)->fileState.topSidebar = newTop;
-
- if (window) {
- BeginContent(window);
- ScrollRect(&(window->portRect), dh, dv, updateRgn = NewRgn());
- EndContent(window);
- }
-
- if (dh)
- (*frHndl)->fileState.leftSidebar = newLeft;
- if (dv)
- (*frHndl)->fileState.topSidebar = newTop;
-
- if (window) {
- if (proc = (*frHndl)->fileState.drawFrameProc) {
- SetOrigin(0, 0);
- (*proc)(frHndl, window); /* Draw the application's portion of the frame. */
- }
- OffsetRgn(((WindowPeek)window)->updateRgn, dh, dv);
- /* We want to add the scrolled-in area into the updateRgn. We
- ** also want to keep the old area. The old update area is
- ** no longer mapped to the same location, due to the scroll,
- ** so offset it by the amount scrolled. Once it is offset, we
- ** can add our new update portion to the updateRgn. */
- GetContentOrigin(window, &contOrg);
- SetOrigin(contOrg.h, contOrg.v);
- InvalRgn(updateRgn);
- DisposeRgn(updateRgn);
-
- SetOrigin(portRct.left, portRct.top);
- SetPort(oldPort);
-
- DoScrollFrame(window, dh, dv); /* There may be changes in the frame due to scrolling. */
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void SetScrollIndentSize(FileRecHndl frHndl, short newh, short newv)
- {
- WindowPtr oldPort, window;
- Rect portRct, rct;
- short oldh, oldv;
- ControlHandle ctl;
- DrawFrameProcPtr proc;
-
- oldPort = SetFilePort(frHndl);
- GetPort(&window);
- portRct = window->portRect;
- SetOrigin(0, -16384);
-
- oldh = (*frHndl)->fileState.hScrollIndent;
- oldv = (*frHndl)->fileState.vScrollIndent;
-
- if (newh != kwNoChange) {
- if (ctl = (*frHndl)->fileState.hScroll) {
- HideControl(ctl);
- rct = (*ctl)->contrlRect;
- rct.left += (newh - oldh);
- MoveControl(ctl, rct.left, rct.top);
- SizeControl(ctl, rct.right - rct.left, 16);
- ShowControl(ctl);
- }
- (*frHndl)->fileState.hScrollIndent = newh;
- }
-
- if (newv != kwNoChange) {
- if (ctl = (*frHndl)->fileState.vScroll) {
- HideControl(ctl);
- rct = (*ctl)->contrlRect;
- rct.top += (newv - oldv);
- MoveControl(ctl, rct.left, rct.top);
- SizeControl(ctl, 16, rct.bottom - rct.top);
- ShowControl(ctl);
- }
- (*frHndl)->fileState.vScrollIndent = newv;
- }
-
- if (proc = (*frHndl)->fileState.drawFrameProc) {
- SetOrigin(0, 0);
- (*proc)(frHndl, window); /* Draw the application's portion of the frame. */
- }
-
- SetOrigin(portRct.left, portRct.top);
- SetPort(oldPort);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- FileRecHndl GetNextDocument(WindowPtr window, OSType sftype)
- {
- if (!(window = GetNextWindow(window, sftype))) return(nil);
- return((FileRecHndl)GetWRefCon(window));
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- WindowPtr GetNextWindow(WindowPtr window, OSType sftype)
- {
- WindowPeek wpeek;
- FileRecHndl frHndl;
-
- if (window == (WindowPtr)-1)
- window = nil;
-
- if (!window)
- #ifdef __SYSEQU__
- wpeek = *(WindowPeek *)WindowList;
- #else
- wpeek = WindowList;
- #endif
- else wpeek = ((WindowPeek)window)->nextWindow;
-
- for (; wpeek; wpeek = wpeek->nextWindow) {
-
- if (wpeek->visible) {
-
- if ((wpeek->windowKind < userKind) && (wpeek->windowKind != dialogKind)) continue;
- /* Not a dialog or user window, so skip it. */
-
- if (!sftype) break;
- /* Request is for any visible window. We have a visible window, so break. */
-
- if (!(frHndl = (FileRecHndl)GetWRefCon((WindowPtr)wpeek))) {
-
- /* Window doesn't have an frHndl in the refcon, so there is only so much that
- ** we can check. See if the request is specific to dialogs or regular windows.
- ** If so, then return the window. */
-
- if (sftype == 'DLOG')
- if (wpeek->windowKind == dialogKind) break;
-
- if (sftype == 'WIND')
- if (wpeek->windowKind >= userKind) break;
-
- continue; /* Doesn't match request, so try next window. */
- }
- if ((*frHndl)->fileState.sfType == sftype) break; /* Bingo. */
- /* The sfType field is the first 4 bytes in the refcon handle. If you don't
- ** want to use the application framework completely, but you still want this
- ** function, then all you have to do is place an identifier in the first 4
- ** bytes of the refcon handle. */
- }
- }
-
- return((WindowPtr)wpeek);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- WindowPtr GetPreviousWindow(WindowPtr window)
- {
- WindowPeek wpeek;
- WindowPtr lastVisWindow;
-
- if (window == (WindowPtr)-1) return((WindowPtr)-1);
-
- #ifdef __SYSEQU__
- wpeek = *(WindowPeek *)WindowList;
- #else
- wpeek = WindowList;
- #endif
-
- for (lastVisWindow = (WindowPtr)-1; wpeek; wpeek = wpeek->nextWindow) {
- if (window == (WindowPtr)wpeek) break;
- if (wpeek->visible)
- lastVisWindow = (WindowPtr)wpeek;
- }
-
- return(lastVisWindow);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoZoomWindow(WindowPtr window, EventRecord *event, short zoomDir)
- {
- Boolean doit;
- Rect old;
- FileRecHndl frHndl;
- short hDocSize, vDocSize;
-
- doit = true;
- if (event)
- doit = TrackBox(window, event->where, zoomDir);
-
- if (doit) {
- old = GetWindowContentRect(window);
- frHndl = (FileRecHndl)GetWRefCon(window);
- hDocSize = (*frHndl)->d.doc.fhInfo.hDocSize;
- hDocSize += (*frHndl)->fileState.leftSidebar;
- if ((*frHndl)->fileState.hScroll)
- hDocSize += 15;
- vDocSize = (*frHndl)->d.doc.fhInfo.vDocSize;
- vDocSize += (*frHndl)->fileState.topSidebar;
- if ((*frHndl)->fileState.vScroll)
- vDocSize += 15;
- ZoomToWindowDevice(window, hDocSize, vDocSize, zoomDir, false);
- DoResizeWindow(window, old.right - old.left, old.bottom - old.top);
- }
- }
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- RgnHandle DoCalcFrameRgn(WindowPtr window)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- RgnHandle urgn, wrgn;
- short i;
- Rect rct;
- Point l2g;
- CalcFrameRgnProcPtr proc;
-
- urgn = NewRgn();
- if (!window) return(urgn);
-
- frHndl = (FileRecHndl)GetWRefCon(window);
- oldPort = SetFilePort(frHndl);
- SetOrigin(0, 0);
-
- if (proc = (*frHndl)->fileState.calcFrameRgnProc)
- (*proc)(frHndl, window, urgn);
-
- wrgn = NewRgn();
- for (i = 0; i < 2; ++i) {
- rct = window->portRect;
- if (i)
- rct.bottom = (*frHndl)->fileState.topSidebar;
- else
- rct.right = (*frHndl)->fileState.leftSidebar;
- RectRgn(wrgn, &rct);
- UnionRgn(urgn, wrgn, urgn);
- }
- DisposeRgn(wrgn);
-
- l2g.h = l2g.v = 0;
- LocalToGlobal(&l2g);
- OffsetRgn(urgn, l2g.h, l2g.v);
-
- wrgn = DoCalcScrollRgn(window);
- UnionRgn(urgn, wrgn, urgn);
- DisposeRgn(wrgn);
-
- SetPort(oldPort);
- return(urgn);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- RgnHandle DoCalcScrollRgn(WindowPtr window)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- RgnHandle urgn, wrgn;
- short attributes, i;
- Boolean growIconSpace;
- Rect rct;
- Point l2g;
- ControlHandle ctl;
-
- urgn = NewRgn();
- if (!window) return(urgn);
-
- frHndl = (FileRecHndl)GetWRefCon(window);
- oldPort = SetFilePort(frHndl);
- SetOrigin(0, 0);
-
- attributes = (*frHndl)->fileState.attributes;
- growIconSpace = (attributes & (kwGrowIcon | (kwHScrollLessGrow - kwHScroll) | (kwVScrollLessGrow - kwVScroll)));
- /* growIconSpace true if window has grow icon or a blank space for one. */
-
- wrgn = NewRgn();
- if (growIconSpace) {
- rct = window->portRect;
- rct.left = rct.right - 15;
- rct.top = rct.bottom - 15;
- RectRgn(wrgn, &rct);
- UnionRgn(urgn, wrgn, urgn);
- }
- for (i = 0; i < 2; ++i) {
- ctl = (i) ? (*frHndl)->fileState.vScroll : (*frHndl)->fileState.hScroll;
- if (ctl) {
- rct = (*ctl)->contrlRect;
- if (i)
- rct.top -= (*frHndl)->fileState.vScrollIndent;
- else
- rct.left -= (*frHndl)->fileState.hScrollIndent;
- OffsetRect(&rct, 0, 16384);
- RectRgn(wrgn, &rct);
- UnionRgn(urgn, wrgn, urgn);
- }
- }
- DisposeRgn(wrgn);
-
- l2g.h = l2g.v = 0;
- LocalToGlobal(&l2g);
- OffsetRgn(urgn, l2g.h, l2g.v);
-
- SetPort(oldPort);
- return(urgn);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
- {
- FileRecHndl frHndl;
- ContentClickProcPtr proc;
-
- if (!IsDAWindow(window))
- if (frHndl = (FileRecHndl)GetWRefCon(window))
- if (proc = (*frHndl)->fileState.contentClickProc)
- (*proc)(window, event, firstClick);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoDragWindow(WindowPtr window, EventRecord *event, Rect bounds)
- {
-
- WindowPtr oldPort, fwindow;
- GrafPort bigPort;
- WindowPeek wpeek;
- FileRecHndl frHndl;
- RgnHandle dragRgn;
- Point offset;
- Rect contRct;
- short wkind;
-
- GetPort(&oldPort);
-
- OpenPort(&bigPort);
- CopyRgn(GetGrayRgn(), bigPort.visRgn);
- bigPort.portRect = (*bigPort.visRgn)->rgnBBox;
-
- fwindow = (WindowPtr)-1;
- if (!IsDAWindow(window)) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- wkind = (*frHndl)->fileState.attributes & (kwIsPalette | kwIsModalDialog);
- fwindow = FrontWindowOfType(wkind);
- for (wpeek = (WindowPeek)FrontWindow(); wpeek; wpeek = wpeek->nextWindow) {
- if (fwindow == (WindowPtr)wpeek) break;
- DiffRgn(bigPort.visRgn, wpeek->strucRgn, bigPort.visRgn);
- }
- }
-
- CopyRgn(((WindowPeek)window)->strucRgn, dragRgn = NewRgn());
- *(long *)&offset = DragGrayRgn(dragRgn, event->where, &bounds, &bounds, noConstraint, nil);
-
- DisposeRgn(dragRgn);
- ClosePort(&bigPort);
- SetPort(oldPort);
-
- if ((offset.h != 0x8000) || (offset.v != 0x8000)) {
- contRct = (*((WindowPeek)window)->contRgn)->rgnBBox;
- OffsetRect(&contRct, offset.h, offset.v);
- MoveWindow(window, contRct.left, contRct.top, false);
- }
-
- if (!(event->modifiers & cmdKey))
- CleanSendInFront(window, fwindow);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoDrawFrame(WindowPtr window)
- {
- FileRecHndl frHndl;
- WindowPtr oldPort;
- short attributes;
- DrawFrameProcPtr proc;
-
- if (window) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- oldPort = SetFilePort(frHndl);
- SetOrigin(0, 0);
-
- attributes = (*frHndl)->fileState.attributes;
- if (attributes & kwGrowIcon)
- DoDrawGrowIcon(window, false, false);
-
- SetOrigin(0, -16384);
- DoDrawControls(window, true);
- SetOrigin(0, 0);
- if (proc = (*frHndl)->fileState.drawFrameProc)
- (*proc)(frHndl, window);
-
- SetPort(oldPort);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoFreeDocument(FileRecHndl frHndl)
- {
- FreeDocumentProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.freeDocumentProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoFreeWindow(FileRecHndl frHndl, WindowPtr window)
- {
- FreeWindowProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.freeWindowProc)
- err = (*proc)(frHndl, window);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoImageDocument(FileRecHndl frHndl)
- {
- ImageProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.imageProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoInitContent(FileRecHndl frHndl, WindowPtr window)
- {
- InitContentProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.initContentProc)
- err = (*proc)(frHndl, window);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- Boolean DoKeyDown(EventRecord *event)
- {
- char key;
- WindowPtr window;
- FileRecHndl frHndl;
- ContentKeyProcPtr proc;
- Boolean passThrough;
-
- key = event->message & charCodeMask;
- if (event->modifiers & cmdKey) { /* If command key down... */
- if (event->what == keyDown) {
- DoAdjustMenus(); /* Prepare menus properly. */
- DoMenuCommand(MenuKey(key));
- }
- return(false);
- }
-
- for (window = nil; window = GetNextWindow(window, 0);) {
- if (frHndl = (FileRecHndl)GetWRefCon(window)) {
- if (proc = (*frHndl)->fileState.contentKeyProc) {
- passThrough = false;
- if ((*proc)(window, event, &passThrough)) return(true);
- if (!passThrough) break;
- }
- }
- }
-
- return(false);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoMouseDown(EventRecord *event)
- {
- WindowPtr window, fwindow;
- FileRecHndl frHndl;
- Rect contentRct, old, growLimits;
- Point pt;
- long size;
- short part, wkind;
-
- gCursorPtr = nil;
- /* No shortcuts when we recalculate the cursor region. */
-
- part = FindWindow(event->where, &window);
-
- frHndl = nil;
- if (window)
- frHndl = (FileRecHndl)GetWRefCon(window);
-
- if (part == inGrow)
- if (!((*frHndl)->fileState.attributes & kwGrowIcon))
- part = inContent;
-
- if (part != inContent)
- DoSetCursor(&qd.arrow);
-
- switch(part) {
-
- case inContent:
-
- if (IsDAWindow(window)) {
- if (window != FrontWindow()) {
- SelectWindow(window);
- HiliteWindows();
- }
- break;
- }
-
- wkind = (*frHndl)->fileState.attributes & (kwIsPalette | kwIsModalDialog);
- fwindow = FrontWindowOfType(wkind);
- /* fwindow guaranteed, since worst case we find ourself. */
-
- if (window == fwindow) {
- DoContentClick(window, event, false);
- break;
- } /* The window is the frontmost of this type, so handle the click. */
-
- CleanSendInFront(window, fwindow);
-
- if ((*frHndl)->fileState.attributes & kwDoFirstClick) {
- DoUpdate(window);
- contentRct = GetWindowContentRect(window);
- if (PtInRect(event->where, &contentRct))
- DoContentClick(window, event, true);
- }
- break;
-
- case inDrag:
- DoDragWindow(window, event, qd.screenBits.bounds);
- break; /* Pass screenBits.bounds to get all gDevices. */
-
- case inGoAway:
- if (TrackGoAway(window, event->where))
- DisposeOneWindow(window, kClose);
- break;
-
- case inGrow:
- old = GetWindowContentRect(window);
- growLimits = (*frHndl)->fileState.windowSizeBounds;
- ++growLimits.right;
- ++growLimits.bottom;
- if (size = GrowWindow(window, event->where, &growLimits)) {
- pt = *(Point *)&size;
- SizeWindow(window, pt.h, pt.v, true);
- DoResizeWindow(window, old.right - old.left, old.bottom - old.top);
- }
- break;
-
- case inMenuBar: /* Process mouse menu command (if any). */
- DoAdjustMenus();
- DoMenuCommand(MenuSelect(event->where));
- break;
-
- case inSysWindow: /* Let the system handle the mouseDown. */
- SystemClick(event, window);
- break;
-
- case inZoomIn:
- case inZoomOut:
- DoZoomWindow(window, event, part);
- break;
-
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Frees up the hierarchical document and undo portions of a default document. */
-
- #pragma segment Window
- OSErr DefaultFreeDocument(FileRecHndl frHndl)
- {
- TreeObjHndl root, undo;
-
- if (root = (*frHndl)->d.doc.root) { /* If we have a valid root object... */
- if (undo = mDerefRoot(root)->undo) /* If we have a valid undo object... */
- DisposeObjAndOffspring(undo); /* Dispose of undo info. */
- DisposeObjAndOffspring(root); /* Dispose of hierarchical file data. */
- (*frHndl)->d.doc.root = nil;
- }
- return(noErr);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoReadDocument(FileRecHndl frHndl)
- {
- ReadDocumentProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.readDocumentProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoReadDocumentHeader(FileRecHndl frHndl)
- {
- ReadDocumentHeaderProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.readDocumentHeaderProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DefaultReadDocument(FileRecHndl frHndl)
- {
- OSErr err;
- TreeObjHndl root, undo;
- short resID, refNum, flags;
- Movie movie;
- Boolean wasChanged;
-
- refNum = (*frHndl)->fileState.refNum;
- err = SetFPos(refNum, fsFromStart, 0);
- /* Set the file position to the beginning of the file. */
-
- err = DoReadDocumentHeader(frHndl);
-
- if (!err) {
- if ((*frHndl)->fileState.sfType == MovieFileType) {
- resID = 0;
- flags = (*frHndl)->fileState.movieFlags;
- err = NewMovieFromFile(&movie, refNum, &resID, nil, flags, &wasChanged);
- if (err) return(err);
- (*frHndl)->fileState.movie = movie;
- (*frHndl)->fileState.movieResID = resID;
- (*frHndl)->fileState.movieDataRefWasChanged = wasChanged;
- }
- else {
- root = (*frHndl)->d.doc.root; /* Preserve the undo field. Preserving */
- undo = mDerefRoot(root)->undo; /* any application-sepcific fields is up */
- /* to the application. */
- err = ReadTree(root, refNum);
- /* Read in the hierarchical file data portion. */
-
- mDerefRoot(root)->undo = undo; /* Restore the 2 over-written fields. */
- mDerefRoot(root)->frHndl = frHndl;
- }
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DefaultReadDocumentHeader(FileRecHndl frHndl)
- {
- short refNum, vers;
- OSErr err;
- char hstate;
- Ptr ptr1, ptr2;
- long count;
-
- if (err = SetFPos(refNum = (*frHndl)->fileState.refNum, fsFromStart, 0)) return(err);
-
- if ((*frHndl)->fileState.sfType != MovieFileType) {
- if (!err) { /* Read header info from file. */
- hstate = HGetState((Handle)frHndl);
- HLock((Handle)frHndl);
- ptr1 = (Ptr)&((*frHndl)->d.doc);
- ptr2 = (Ptr)&((*frHndl)->d.doc.fhInfo.endDocHeaderInfo);
- count = (long)ptr2 - (long)ptr1;
- err = FSRead(refNum, &count, ptr1);
- HSetState((Handle)frHndl, hstate);
- if (!err) {
- vers = (*frHndl)->d.doc.fhInfo.version;
- if ((vers < gMinVersion) || (vers > gMaxVersion))
- err = kWrongVersion;
- }
- }
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DefaultReadDocumentFixup(FileRecHndl frHndl)
- {
- TreeObjHndl root, undo, chndl;
-
- root = (*frHndl)->d.doc.root;
- undo = mDerefRoot(root)->undo;
- for (;;) {
- if (!(chndl = GetChildHndl(root, -1))) break;
- if ((*chndl)->type != UNDOTASKOBJ) break;
- MoveChild(NO_EDIT, root, -1, undo, -1);
- mDerefUndo(undo)->undoDepth++;
- /* Move any undo tasks that were saved with the document
- ** out of the document and into the undo hierarchy. */
- }
-
- return(noErr);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoWriteDocument(FileRecHndl frHndl)
- {
- WriteDocumentProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.writeDocumentProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DoWriteDocumentHeader(FileRecHndl frHndl)
- {
- WriteDocumentHeaderProcPtr proc;
- OSErr err;
-
- err = noErr;
- if (proc = (*frHndl)->fileState.writeDocumentHeaderProc)
- err = (*proc)(frHndl);
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DefaultWriteDocument(FileRecHndl frHndl)
- {
- short refNum, cnum, numSaveUndos;
- OSErr err;
- long fpos;
- TreeObjHndl root, undo, chndl;
-
- refNum = (*frHndl)->fileState.refNum;
- err = DoWriteDocumentHeader(frHndl);
-
- if (!err) {
- if ((*frHndl)->fileState.sfType != MovieFileType) {
- undo = GetUndoHndl(root = (*frHndl)->d.doc.root);
- numSaveUndos = mDerefUndo(undo)->numSaveUndos;
- for (cnum = mDerefUndo(undo)->undoDepth; ((cnum) && (numSaveUndos)); --numSaveUndos)
- MoveChild(NO_EDIT, undo, --cnum, root, -1);
- /* Move the designated number of undo tasks into the document side.
- ** They will be saved to disk this way. The designated number may be
- ** zero, which means that undos aren't saved to disk. */
-
- DoNumberTree(root);
- /* Assign each object in the tree a unique treeID. This will allow
- ** the objects to convert handle references into ID references so that
- ** the data can be saved to disk. */
-
- err = WriteTree(root, refNum);
- /* Write out the hierarchical data. This includes the data in the root object.
- ** When reading a data file, the root object data has already been initialized,
- ** and therefore reading in the old root data from disk is a bad thing. This is
- ** handled by caching the root data prior to reading in a file, and then
- ** restoring the data after the ReadTree() call has been made. */
-
- for (;;) {
- if (!(chndl = GetChildHndl(root, -1))) break;
- if ((*chndl)->type != UNDOTASKOBJ) break;
- MoveChild(NO_EDIT, root, -1, undo, cnum++);
- /* Move any undo tasks that we previously moved into the document
- ** out of the document and back into the undo hierarchy. */
- }
-
- if (!err) {
- err = GetFPos(refNum, &fpos);
- if (!err)
- err = SetEOF(refNum, fpos);
- } /* The document may be shorter than last time it was written to disk.
- ** Handle this case by ending the file based on the new length. */
- }
- else {
- }
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- OSErr DefaultWriteDocumentHeader(FileRecHndl frHndl)
- {
- short refNum;
- OSErr err;
- WindowPtr window;
- char hstate;
- Ptr ptr1, ptr2;
- long count;
-
- if (err = SetFPos(refNum = (*frHndl)->fileState.refNum, fsFromStart, 0)) return(err);
-
- if ((*frHndl)->fileState.sfType != MovieFileType) {
- if (window = (*frHndl)->fileState.window) {
- if (!(*frHndl)->fileState.readOnly) {
- (*frHndl)->d.doc.fhInfo.structureRect = GetWindowStructureRect(window);
- (*frHndl)->d.doc.fhInfo.contentRect = GetWindowContentRect(window);
- (*frHndl)->d.doc.fhInfo.stdState = mDerefWStateData(window)->stdState;
- (*frHndl)->d.doc.fhInfo.userState = mDerefWStateData(window)->userState;
- }
- }
- else SetRect(&(*frHndl)->d.doc.fhInfo.structureRect, 0, 0, 0, 0);
- hstate = HGetState((Handle)frHndl);
- HLock((Handle)frHndl);
- ptr1 = (Ptr)&((*frHndl)->d.doc);
- ptr2 = (Ptr)&((*frHndl)->d.doc.fhInfo.endDocHeaderInfo);
- count = (long)ptr2 - (long)ptr1;
- err = FSWrite(refNum, &count, ptr1);
- HSetState((Handle)frHndl, hstate);
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoResizeContent(WindowPtr window, short oldh, short oldv)
- {
- FileRecHndl frHndl;
- ResizeContentProcPtr proc;
-
- if (IsAppWindow(window))
- if (frHndl = (FileRecHndl)GetWRefCon(window))
- if (proc = (*frHndl)->fileState.resizeContentProc)
- (*proc)(window, oldh, oldv);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoScrollFrame(WindowPtr window, long dh, long dv)
- {
- FileRecHndl frHndl;
- ScrollFrameProcPtr proc;
-
- if (window) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- if (proc = (*frHndl)->fileState.scrollFrameProc)
- (*proc)(frHndl, window, dh, dv);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoUndoFixup(FileRecHndl frHndl, Point contOrg, Boolean afterUndo)
- {
- UndoFixupProcPtr proc;
-
- if (proc = (*frHndl)->fileState.undoFixupProc)
- (*proc)(frHndl, contOrg, afterUndo);
- }
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- WindowPtr GetOldLocWindow(short id, Ptr storage, Boolean vis, WindowPtr relWindow,
- WindowPtr behind, Boolean inColor, Rect sizeInfo, long refCon)
- {
- return(GetSomeKindOfWindow(OldLocWindow, id, storage, vis, relWindow,
- behind, inColor, sizeInfo, refCon));
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Open a window where it was stored at. This would be simple, except for the
- ** complication that the user may be opening the document on a different Mac
- ** that doesn't have the same monitor configuration. Due to this, we need to
- ** make sure that the window doesn't open completely out of view. */
-
- #pragma segment Window
- static Rect OldLocWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo)
- {
- FileRecHndl frHndl;
- RgnHandle rgn;
- Rect rct, bbox, srct, crct;
- short attributes, hDocSize, vDocSize, dh, dv, h, v;
-
- frHndl = (FileRecHndl)GetWRefCon(window);
- attributes = (*frHndl)->fileState.attributes;
-
- if (hDocSize = (*frHndl)->d.doc.fhInfo.hDocSize) {
- hDocSize += (*frHndl)->fileState.leftSidebar;
- if (attributes & kwHScroll)
- hDocSize += 15;
- }
- if (vDocSize = (*frHndl)->d.doc.fhInfo.vDocSize) {
- vDocSize += (*frHndl)->fileState.topSidebar;
- if (attributes & kwVScroll)
- vDocSize += 15;
- }
-
- srct = (*frHndl)->d.doc.fhInfo.structureRect;
- crct = (*frHndl)->d.doc.fhInfo.contentRect;
- if (EmptyRect(&srct)) {
- SetRect(&sizeInfo, hDocSize, vDocSize, hDocSize, vDocSize);
- return(StaggerWindow(window, relatedWindow, sizeInfo));
- }
-
- rct = srct;
- rct.bottom = crct.top;
-
- RectRgn(rgn = NewRgn(), &rct);
- SectRgn(rgn, GetGrayRgn(), rgn);
- bbox = (*rgn)->rgnBBox;
- DisposeRgn(rgn);
-
- if (EqualRect(&rct, &bbox)) {
- rct = (*frHndl)->d.doc.fhInfo.contentRect;
- SizeWindow(window, rct.right - rct.left, rct.bottom - rct.top, false);
- MoveWindow(window, rct.left, rct.top, false);
- mDerefWStateData(window)->stdState = (*frHndl)->d.doc.fhInfo.stdState;
- mDerefWStateData(window)->userState = (*frHndl)->d.doc.fhInfo.userState;
- return(rct);
- }
-
- rct = srct;
- if (!(dh = bbox.left - rct.left))
- dh = bbox.right - rct.right;
- if (!(dv = bbox.top - rct.top))
- dv = bbox.bottom - rct.bottom;
- OffsetRect(&rct, dh, dv);
-
- RectRgn(rgn = NewRgn(), &rct);
- SectRgn(rgn, GetGrayRgn(), rgn);
- bbox = (*rgn)->rgnBBox;
- DisposeRgn(rgn);
-
- if (EqualRect(&rct, &bbox)) {
- h = crct.right - crct.left;
- v = crct.bottom - crct.top;
- }
- else {
- h = hDocSize;
- v = vDocSize;
- }
- SetRect(&sizeInfo, h, v, h, v);
-
- return(StaggerWindow(window, relatedWindow, sizeInfo));
- /* Force window as big as possible for this screen and data size. */
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void CleanSendBehind(WindowPtr window, WindowPtr afterWindow)
- {
- WindowPtr oldPort;
- Point offset;
- RgnHandle contRgn, keepContRgn, visRgn;
-
- if (afterWindow == (WindowPtr)-1) {
- BringToFront(window);
- HiliteWindows();
- return;
- }
-
- GetPort(&oldPort);
- SetPort(window);
- offset.h = offset.v = 0;
- LocalToGlobal(&offset);
- SetPort(window);
-
- CopyRgn(contRgn = ((WindowPeek)window)->contRgn, keepContRgn = NewRgn());
- OffsetRgn(visRgn = window->visRgn, offset.h, offset.v);
- DiffRgn(contRgn, visRgn, contRgn);
- OffsetRgn(visRgn, -offset.h, -offset.v);
- /* Don't allow PaintOne to touch the part of the window already visible. */
-
- SendBehind(window, afterWindow);
- /* Do the SendBehind. Since the content region is way off the
- ** screen(s), no erasing of the content of the window will occur. */
-
- CopyRgn(keepContRgn, contRgn);
- DisposeRgn(keepContRgn);
-
- CalcVis((WindowPeek)window);
- /* One negative to the content region games is that the visRgn gets
- ** calculated incorrectly when SendBehind() is called. The call to
- ** CalcVis() fixes this problem. */
-
- HiliteWindows();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void CleanSendInFront(WindowPtr window, WindowPtr beforeWindow)
- {
- beforeWindow = GetPreviousWindow(beforeWindow);
- CleanSendBehind(window, beforeWindow);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void HiliteWindows(void)
- {
- WindowPtr window;
- FileRecHndl frHndl;
- short thisKind, lastKind;
-
- lastKind = -1; /* No such kind. */
-
- for (window = FrontWindow(); window; window = (WindowPtr)(((WindowPeek)window)->nextWindow)) {
-
- if (!((WindowPeek)window)->visible) continue;
-
- thisKind = kwIsDocument;
- if (IsAppWindow(window)) {
- frHndl = (FileRecHndl)GetWRefCon(window);
- thisKind = (*frHndl)->fileState.attributes & (kwIsPalette | kwIsModalDialog);
- }
-
- if (gInBackground)
- lastKind = thisKind;
- /* If application moved to background, we want to turn all hilighting off
- ** for all windows. This is accomplished by pretending that the kind of
- ** the current window is the same as the kind of the last window. */
-
- if (thisKind != lastKind) {
- if (!((WindowPeek)window)->hilited) {
- HiliteWindow(window, true);
- DoActivate(window);
- }
- lastKind = thisKind;
- }
- else {
- if (((WindowPeek)window)->hilited) {
- HiliteWindow(window, false);
- DoActivate(window);
- }
- }
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void UnhiliteWindows(void)
- {
- WindowPtr window;
-
- for (window = nil; window = GetNextWindow(window, 0);) {
- if (!((WindowPeek)window)->visible) continue;
- if (((WindowPeek)window)->windowKind <= dialogKind) continue;
- if (((WindowPeek)window)->hilited) {
- HiliteWindow(window, false);
- DoActivate(window);
- }
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This is called when an update event is received for a window. First, the
- ** updateRgn is separated into two parts. Part 1 holds the window frame area,
- ** if any. This is the area that might hold the scrollbars, grow icon, and
- ** any other application-specific frame parts. This is drawn first. Once
- ** this is done, the remainder of the updateRgn is drawn. This allows us to
- ** handle all of the frame clipping without using the clipRgn. By freeing up
- ** the clipRgn, we allow the application to use it without having to share. */
-
- #pragma segment Window
- void DoUpdate(WindowPtr window)
- {
- RgnHandle contPart, framePart;
- Point contOrg;
- FileRecHndl frHndl;
-
- SetPort(window);
-
- if (IsAppWindow(window)) {
-
- DoUpdateSeparate(window, &contPart, &framePart);
-
- if (framePart) { /* Update the document frame, if any. */
- CopyRgn(framePart, ((WindowPeek)window)->updateRgn);
- DisposeRgn(framePart);
- ++gBeginUpdateNested;
- BeginUpdate(window);
- DoDrawFrame(window);
- EndUpdate(window);
- --gBeginUpdateNested;
- }
- if (contPart) { /* Update the rest of the content. */
- CopyRgn(contPart, ((WindowPeek)window)->updateRgn);
- DisposeRgn(contPart);
- ++gBeginUpdateNested;
- BeginUpdate(window);
- GetContentOrigin(window, &contOrg);
- SetOrigin(contOrg.h, contOrg.v);
- frHndl = (FileRecHndl)GetWRefCon(window);
- DoImageDocument(frHndl);
- SetOrigin(0, 0);
- EndUpdate(window);
- --gBeginUpdateNested;
- }
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoSetCursor(Cursor *cursor)
- {
- gCursorPtr = nil;
-
- if (cursor)
- SetCursor(cursor);
-
- if (!cursor)
- DoCursor();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- CursPtr DoSetResCursor(short crsrID)
- {
- CursHandle crsr;
-
- gCursorPtr = nil;
-
- crsr = GetCursor(crsrID);
- if (crsr) {
- gCursor = **crsr;
- DoSetCursor(&gCursor);
- return(&gCursor);
- }
-
- SetCursor(&qd.arrow);
- return(&qd.arrow);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DoWindowCursor(void)
- {
- WindowPeek wpeek;
- WindowPtr window;
- Point mouseLoc;
- FileRecHndl frHndl;
- RgnHandle srgn;
- WindowCursorProcPtr proc;
-
- if (gInBackground) return;
- /* Don't change cursors if we aren't the front application. */
-
- if (!gCursorRgn)
- gCursorRgn = NewRgn();
-
- wpeek = (WindowPeek)FrontWindow();
- if (!IsAppWindow((WindowPtr)wpeek)) {
- SetRectRgn(gCursorRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
- SetCursor(gCursorPtr = &qd.arrow);
- } /* Non-application windows get an arrow cursor. */
-
- mouseLoc = GetGlobalMouse();
-
- if (gCursorPtr) { /* Do we already have a cursor... */
- if (PtInRgn(mouseLoc, gCursorRgn)) { /* Are we still in the cursor area... */
- SetCursor(gCursorPtr); /* Then set it to that. */
- return;
- }
- }
-
- SetRectRgn(gCursorRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
-
- for (wpeek = (WindowPeek)FrontWindow();; wpeek = wpeek->nextWindow) {
-
- if (!IsAppWindow((WindowPtr)wpeek)) break;
-
- if (!wpeek->visible) continue; /* No cursors for invisible windows. */
-
- window = (WindowPtr)wpeek;
- srgn = wpeek->strucRgn;
- frHndl = (FileRecHndl)GetWRefCon(window);
-
- if (!(proc = (*frHndl)->fileState.windowCursorProc)) {
- if (PtInRgn(mouseLoc, srgn)) {
- SectRgn(gCursorRgn, srgn, gCursorRgn);
- SetCursor(gCursorPtr = &qd.arrow);
- return;
- }
- }
- else if ((*proc)(frHndl, window, mouseLoc)) return;
-
- DiffRgn(gCursorRgn, srgn, gCursorRgn);
- }
-
- SetCursor(gCursorPtr = &qd.arrow);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- WindowPtr FrontWindowOfType(short wkind)
- {
- WindowPtr window;
- FileRecHndl frHndl;
- short wk;
-
- for (window = nil; window = GetNextWindow(window, 0);) {
- if (frHndl = (FileRecHndl)GetWRefCon(window)) {
- wk = (*frHndl)->fileState.attributes & (kwIsPalette | kwIsModalDialog);
- if (wkind == wk) return(window);
- }
- }
-
- return(nil);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- short HCenteredAlert(short alertID, WindowPtr relatedWindow, ModalFilterProcPtr filter)
- {
- short itemHit;
-
- UnhiliteWindows();
- itemHit = CenteredAlert(alertID, relatedWindow, filter);
- HiliteWindows();
- return(itemHit);
- }
-
-
-
-